﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Sqlite;

namespace DemoOkami
{
	class tabSettings 
	{
		/// <summary>
		/// The mdtemp is object of MyDatabase class.
		/// </summary>
		SqliteDB mdTemp;
		/// <summary>
		/// The txtName, txtAge, txtCountry are object of EditText.
		/// </summary>
		EditText txtUser, txtPass, txtAllowS;
		/// <summary>
		/// The tvMsg is object of TextView.
		/// </summary>
		TextView tvMsg;
		/// <summary>
		/// The imgAdd, imgEdit, imgDelete, imgFind are object of ImageButton
		/// </summary>
		ImageButton imgAdd, imgEdit, imgDelete, imgFind,imgAllow;
		View Mainview;

		Context MainContext;
		public tabSettings(Context MainContext)
		{
			this.MainContext = MainContext;
		}

		public void GetTab(ref GenericFragmentPagerAdaptor adaptor)
		{
			string strResult = string.Empty;
			adaptor.AddFragmentView((i, v, b) =>
				{
					var view = i.Inflate(Resource.Layout.usersSettings, v, false);
					Mainview=view;
					// initialization of database class object.
					mdTemp = new SqliteDB ("usuarios_DB");
					// get ImageButton object instance from resource.
					imgAdd = view.FindViewById<ImageButton> (Resource.Id.imgAdd);
					imgEdit = view.FindViewById<ImageButton> (Resource.Id.imgEdit);
					imgDelete = view.FindViewById<ImageButton> (Resource.Id.imgDelete);
					imgFind = view.FindViewById<ImageButton> (Resource.Id.imgFind);
					imgAllow = view.FindViewById<ImageButton> (Resource.Id.imgAllow);
					// set images on image button from resource.
					imgAdd.SetImageResource (Resource.Drawable.ic_action_save);
					imgEdit.SetImageResource (Resource.Drawable.ic_action_edit);
					imgDelete.SetImageResource (Resource.Drawable.ic_action_remove);
					imgFind.SetImageResource (Resource.Drawable.ic_action_search); 
					imgAllow.SetImageResource (Resource.Drawable.ic_action_not_important); 
					// get EditText object instance from resource.
					txtUser = view.FindViewById<EditText> (Resource.Id.txtUser);
					txtPass = view.FindViewById<EditText> (Resource.Id.txtPass);
					txtAllowS = view.FindViewById<EditText> (Resource.Id.txtAllowS);
					txtAllowS.Text="0";
					tvMsg = view.FindViewById<TextView> (Resource.Id.tvMsg);

					tvMsg.Text = mdTemp.Message;
					// create ImageButton click event for imgAdd, imgEdit, imgDelete, imgFind. 
					imgAdd.Click += delegate {
						//database call add record function AddRecord().
						mdTemp.AddRecord (txtUser.Text, txtPass.Text ,int.Parse (txtAllowS.Text));
						tvMsg.Text = mdTemp.Message;
						txtUser.Text = txtPass.Text = txtAllowS.Text = "";
						GetCursorView();
					};

					imgEdit.Click += delegate {
						int iId = -1;
						int.TryParse (tvMsg.Text, out iId);
						//database call update record function UpdateRecord().
						mdTemp.UpdateRecord (iId, txtUser.Text, txtPass.Text ,int.Parse (txtAllowS.Text));
						tvMsg.Text = mdTemp.Message;
						txtUser.Text = txtPass.Text = txtAllowS.Text = "";
						GetCursorView();
					};
					imgDelete.Click += delegate {
						int iId = -1;
						int.TryParse (tvMsg.Text, out iId);
						//database call delete record function DeleteRecord().
						mdTemp.DeleteRecord (iId);
						tvMsg.Text = mdTemp.Message;
						txtUser.Text = txtPass.Text = txtAllowS.Text = "";
						GetCursorView();
					};
					imgFind.Click += delegate {

						//database call search record function GetCursorView().
						string sColumnName = "";
						if (txtUser.Text.Trim () != "") {
							sColumnName = "user";
							GetCursorView (sColumnName, txtUser.Text.Trim ());
						} else
							if (txtPass.Text.Trim () != "") {
								sColumnName = "pass";
								GetCursorView (sColumnName, txtPass.Text.Trim ());
							} else
							{
								GetCursorView ();
								sColumnName = "All";
							}
						tvMsg.Text = "Search by " + sColumnName + ".";

					};
					imgAllow.Click += delegate {
						ChangeStatus();
					};

					// get ListView object instance from resource and add ItemClick, EventHandler.
					ListView lvTemp = view.FindViewById<ListView> (Resource.Id.lvTemp);        
					lvTemp.ItemClick += new EventHandler<AdapterView.ItemClickEventArgs> (ListView_ItemClick);

					return view;
				}
			);
		}

		private void Search()
		{
			GetCursorView ();
		}

		private void ChangeStatus()
		{
			if (txtAllowS.Text=="0") {
				txtAllowS.Text="1";
				imgAllow.SetImageResource (Resource.Drawable.ic_action_important); 
			}
			else {
				txtAllowS.Text="0";
				imgAllow.SetImageResource (Resource.Drawable.ic_action_not_important); 
			}

		}
		private void ValidateStatus()
		{
			if (txtAllowS.Text=="0") {
				imgAllow.SetImageResource (Resource.Drawable.ic_action_not_important); 
			}
			else {
				imgAllow.SetImageResource (Resource.Drawable.ic_action_important); 
			}
		}


		/// <summary>
		/// Lists the view_ item click.
		/// </summary>
		/// <param name='sender'>
		/// object sender.
		/// </param>
		/// <param name='e'>
		/// ItemClickEventArgs e.
		/// </param>
		void ListView_ItemClick (object sender, AdapterView.ItemClickEventArgs e)
		{
			// get TextView object instance from resource layout record_view.axml.
			TextView tvIdShow = e.View.FindViewById<TextView> (Resource.Id.tvIdShow);
			TextView tvUserShow = e.View.FindViewById<TextView> (Resource.Id.tvUserShow);
			TextView tvPassShow = e.View.FindViewById<TextView> (Resource.Id.tvPassShow);
			TextView tvAllowSShow = e.View.FindViewById<TextView> (Resource.Id.tvAllowSShow);
			// read value and wirte in EditText object.
			txtUser.Text = tvUserShow.Text;
			txtPass.Text = tvPassShow.Text;
			txtAllowS.Text = tvAllowSShow.Text;
			//record id is write in TextView object to update or delete record.
			tvMsg.Text = tvIdShow.Text;
			ValidateStatus ();
		}


		/// <summary>
		/// Gets the cursor view to show all record.
		/// </summary>
		protected void GetCursorView ()
		{
			Android.Database.ICursor icTemp = mdTemp.GetRecordCursor ();
			if (icTemp != null) {
				icTemp.MoveToFirst ();
				ListView lvTemp = Mainview.FindViewById<ListView> (Resource.Id.lvTemp);
				string[] from = new string[] {"_id","user","pass","allowS" };
				int[] to = new int[] {
					Resource.Id.tvIdShow,
					Resource.Id.tvUserShow,
					Resource.Id.tvPassShow,
					Resource.Id.tvAllowSShow
				};
				// creating a SimpleCursorAdapter to fill ListView object.
				SimpleCursorAdapter scaTemp = new SimpleCursorAdapter (MainContext, Resource.Layout.record_view, icTemp, from, to);
				lvTemp.Adapter = scaTemp;
			} else {
				tvMsg.Text = mdTemp.Message;
			}
		}
		/// <summary>
		/// Gets the cursor view.
		/// </summary>
		/// <param name='sColumn'>
		/// column filed of MyTable is Name,Age,Country.
		/// </param>
		/// <param name='sValue'>
		/// Value as user input.
		/// </param>
		protected void GetCursorView (string sColumn, string sValue)
		{
			Android.Database.ICursor icTemp = mdTemp.GetRecordCursor (sColumn, sValue);
			if (icTemp != null) {
				icTemp.MoveToFirst ();
				ListView lvTemp = Mainview.FindViewById<ListView> (Resource.Id.lvTemp);
				string[] from = new string[] {"_id","user","pass","allowS" };
				int[] to = new int[] {
					Resource.Id.tvIdShow,
					Resource.Id.tvUserShow,
					Resource.Id.tvPassShow,
					Resource.Id.tvAllowSShow
				};
				// creating a SimpleCursorAdapter to fill ListView object.
				SimpleCursorAdapter scaTemp = new SimpleCursorAdapter (MainContext, Resource.Layout.record_view, icTemp, from, to);
				lvTemp.Adapter = scaTemp;
			} else {
				tvMsg.Text = mdTemp.Message;
			}
		}


	}
}

